/*
 * Copyright (c) 2017. All  rights reserved.
 * 项目名：microservice-base
 * 文件名：BaseController.java
 * Date  ：17-12-4 上午10:57
 * Author：abin
 *
 */

package com.microservice.base.controller;

import com.microservice.base.common.SpecialDateEditor;
import com.microservice.base.entity.Menu;
import com.microservice.base.entity.User;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;


public class BaseController {

    Logger logger = Logger.getLogger(this.getClass());

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected User loginUser;

    @ModelAttribute
    protected void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        if (this.session != null && this.session.getAttribute("loginUser") != null) {
            loginUser = (User) session.getAttribute("loginUser");
        }

        // 根据当前url自动定位顶部及左侧菜单
        String curl = request.getRequestURI().replaceFirst(request.getContextPath(), "");
        if (loginUser != null) {
            Map<String, Menu> menuMap = loginUser.getMenuMap();
            curl = generateCurl(curl, menuMap);
        }
        request.setAttribute("curl", curl);
        request.setAttribute("fullCurl", request.getRequestURI().replaceFirst(request.getContextPath(), ""));
    }

    private String generateCurl(String curl, Map<String, Menu> menuMap) {
        if (StringUtils.isBlank(curl)) {
            return "/";
        }

        if (!curl.equals("/") && curl.indexOf("/") != -1 && !menuMap.containsKey(curl)) {
            curl = curl.substring(0, curl.lastIndexOf("/"));
            return generateCurl(curl, menuMap);
        }
        return curl;
    }

    protected String getIp() {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

    protected String extractPathFromPattern() {
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {//处理特殊数据类型的转换
        binder.registerCustomEditor(Date.class, new SpecialDateEditor());
    }


}
